<template>
  <div>
    <el-dialog title="编辑列表" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" clearable placeholder="必选项">
            <el-option
              v-for="item in rOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="did">
          <el-select v-model="form.did" clearable placeholder="选择部门">
            <el-option
              v-for="item in didList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="Status" placeholder="请选择状态" @change="change">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleOk('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {findById, getAllRoleList, getRoleList, update} from "@/api/sales";
  import {list} from "@/api/company";

  export default {
    name: "edit",
    data() {
      return {
        dialogVisible: false,
        form: {
          id: "",
          name: "",
          phone: "",
          remark: "",
          status: "",
          did: '',
          role: ''
        },
        Status: "",
        rOptions: [],
        didList: [],
        //状态数据
        options: [
          {
            value: "1",
            label: "离职",
          },
          {
            value: "0",
            label: "在职",
          },
        ],
        //表单验证
        rules: {
          name: [{required: true, message: "请输入昵称", trigger: "blur"}],
          phone: [{required: true, message: "请输入手机号", trigger: "blur"}],
          role: [{required: true, message: "请选择角色", trigger: "blur"}],
          did: [{required: true, message: "请选择部门", trigger: "blur"}],
        },
      };
    },
    methods: {
      show(id) {
        this.dialogVisible = true;
        console.log(id);
        let a = {
          id: id,
        };
        getRoleList().then((res) => {
          this.rOptions = res.result;
        });
        // 获取部门列表
        let form1 = {
          page: 1,
          size: 40,
        };
        list(form1).then((res) => {
          console.log(res);
          this.didList = res.result.records;
        })
        // this.Didlist()
        findById(a).then((res) => {
          if (res.status === 200) {
            console.log(res.result);
            this.form.id = res.result.id;
            this.form.name = res.result.name;
            this.form.phone = res.result.phone;
            this.form.remark = res.result.remark;
            this.form.role = res.result.role;
            this.form.did = res.result.did
            if (res.result.status == 0) {
              this.Status = "在职";
              this.form.status = 0;
            } else {
              this.Status = "离职";
              this.form.status = 1;
            }
          }
        });
      },
      handleOk(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            if (this.form.phone != "") {
              var phone = /^1[3|4|5|7|8][0-9]\d{8}$/;
              if (!phone.test(this.form.phone)) {
                this.$message.warning("手机号格式错误, 请重新输入");
                return false;
              }
            }
            update(this.form).then((res) => {
              if (res.status === 200) {
                this.$emit("Ok");
                this.dialogVisible = false;
                this.$message.success("编辑成功");
              }
            });
          }
        });
      },
      change(e) {
        console.log(e);
        this.form.status = e;
      },
    },
  };
</script>

<style scoped></style>
